//
//  UIViewController+ICJSJump.m
//  ICome
//
//  Created by zhangrongwu on 2016/11/21.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "UIViewController+ICJSJump.h"
#import "NSDictionary+URL.h"
#import "ICHeaderFile.h"
@implementation UIViewController (ICJSJump)
// url 解析
//@"http://www.baidu.com/id/s/s?tn=baiduhome_pg&bs=NSRUL&f=8&rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709"
//    NSLog(@"Scheme: %@", [url scheme]);  结果:Scheme: http
//    NSLog(@"Host: %@", [url host]);  结果:Host: www.baidu.com
//    NSLog(@"Port: %@", [url port]);  结果:Port: (null)
//    NSLog(@"Path: %@", [url path]); 结果:Path: /s
- (void)pushCustomViewControllerURL:(NSString *)URL
                 fromViewController:(UIViewController *)ViewController
                           animated:(BOOL)animated {
    
    if ([URL hasPrefix:@"icome"]) { // 打开本地自定义界面
        NSString *tempUrl = [URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *pushUrl = [NSURL URLWithString:tempUrl];
        //NSString *scheme = pushUrl.scheme;
        NSString *host = pushUrl.host;
        //        NSString *path = pushUrl.path;
        NSString *query = pushUrl.query;
        NSString *class = host;
        
        
        //        NSString *coustId = nil;
        //        if (!kStringIsEmpty(path)) {
        //            coustId = [[path componentsSeparatedByString:@"/"] objectAtIndex:1]; //
        //        }
        
        if (![ICTools stringEmpty:(query)]) {
           NSDictionary *queryDict = [NSDictionary dictionaryWithURLQuery:query];
            // 日程
            if ([class isEqualToString:@"schedule"]) {
                NSDictionary *param = nil;
                if (![ICTools stringEmpty:(queryDict[@"beginDate"])]) {
                    param = @{@"beginDate":queryDict[@"beginDate"]};
                }
             
            }
            // 视频会议
            else if ([class isEqualToString:@"videoconf"]) {
                if (queryDict[@"vcId"]) {
                   
                } else {
                   
                }
            }
            
            // 机器人
            else if ([class isEqualToString:@"robot"]) {
                NSString *eId = queryDict[@"eid"];
                if (![ICTools stringEmpty:eId]) {
                    NSString *gId = [ICMessageDatabase usergIdWitheId:eId];
                    if ([ICMessageDatabase isGroupExisted:gId]) {
                        
                        [ICMessageDatabase changeGroupLocalType:1 GroupId:gId];
                        [ICMessageDatabase getGroupWithgId:gId finish:^(NSArray *array, NSError *error) {
                            
                            ICGroup *group = [array firstObject];
                            NSDictionary *dict = @{@"group":group};
                            UIViewController * viewController = [[IComMediator sharedInstance] IComMediator_ChatViewController:dict];
                            [ViewController.navigationController pushViewController:viewController animated:YES];
                            
                        }];
                        
                    } else {
                        NSDictionary *param = @{@"eId":queryDict[@"eid"]};
                        UIViewController *vc = [[IComMediator sharedInstance] IComMediator_RobotDetailViewController:param];
                        [ViewController.navigationController pushViewController:vc animated:YES];
                    }
                }
            }
        }
        
        // 邮箱
        if ([class isEqualToString:@"email"]) {
            if ([ICTools stringEmpty:[ICUser currentUser].email]) {
                ALERT(@"邮箱账号异常");
            } else {
              
            }
        }
        
        return;
    }
    
    if (![ICTools stringEmpty:URL] && ![ICTools stringEmpty:[ICTools getJSTicket]]) {
        if(![URL hasPrefix:@"http"])
        {
            URL = [NSString stringWithFormat:@"http://%@",URL];
        }
        ICWebViewController *vc = [ICWebViewController jsBridgeWKWebViewWithPath:[ICJSBridgeTools appendUserInfoforJSURL:URL] title:@""];
        vc.originalURL = URL;
        [ViewController.navigationController pushViewController:vc animated:animated];
    } else {
        __weak typeof(ViewController) weakSelf = ViewController;
        [ICJSBridgeTools getJsTicket:^(BOOL isSuccess, id object) {
            if (isSuccess) {
                ICWebViewController *vc = [ICWebViewController jsBridgeWKWebViewWithPath:[ICJSBridgeTools appendUserInfoforJSURL:URL] title:@""];
                vc.originalURL = URL;
                [weakSelf.navigationController pushViewController:vc animated:animated];
            }
        }];
    }
}





@end
